package com.ebupt.migu.music.hangupsms.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EmptyUtils;
import com.ebupt.migu.music.hangupsms.config.HangUpSmsConfig;
import com.ebupt.migu.music.hangupsms.entity.ProvinceSwitch;
import com.ebupt.migu.music.hangupsms.service.IProvinceSwitchService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/provinceSwitch")
@Slf4j
public class ProvinceSwitchController {
	@Autowired
	private IProvinceSwitchService provinceSwitchService;
	@Autowired
	private HangUpSmsConfig hangUpSmsConfig;
	/**
	 * 分页列表查询
	 *
	 * @param provinceSwitch
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public ResultObject<IPage<ProvinceSwitch>> queryPageList(ProvinceSwitch provinceSwitch,
														@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														HttpServletRequest req) {
		QueryWrapper<ProvinceSwitch> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(!EmptyUtils.isEmpty(provinceSwitch.getProvinceCode())&& !Arrays.asList(provinceSwitch.getProvinceCode().split(",")).contains("00"),ProvinceSwitch::getProvinceCode,Arrays.asList(provinceSwitch.getProvinceCode().split(",")));
		Page<ProvinceSwitch> page = new Page<ProvinceSwitch>(pageNo, pageSize);
		IPage<ProvinceSwitch> pageList = provinceSwitchService.page(page, queryWrapper);
		return ResultObject.ok(pageList);
	}

	@GetMapping(value = "/province")
	public ResultObject<List<ProvinceSwitch>> province(HttpServletRequest request) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.select("distinct province_name,province_code");
		List list = provinceSwitchService.list(wrapper);
		return ResultObject.ok(list);
	}
	
	/**
	 *   添加
	 *
	 * @param provinceSwitch
	 * @return
	 */
	@PostMapping(value = "/add")
	public ResultObject<String> add(@RequestBody ProvinceSwitch provinceSwitch) {
		provinceSwitchService.save(provinceSwitch);
		hangUpSmsConfig.initSwitch();
		return ResultObject.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param provinceSwitch
	 * @return
	 */
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public ResultObject<String> edit(@RequestBody ProvinceSwitch provinceSwitch) {
		provinceSwitchService.updateById(provinceSwitch);
		boolean matches = "1".matches(hangUpSmsConfig.getCallerNoSendGIndex());
		hangUpSmsConfig.initSwitch();
		return ResultObject.ok("编辑成功!");
	}

	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public ResultObject<String> delete(@RequestParam(name="id",required=true) String id) {
		provinceSwitchService.removeById(id);
		return ResultObject.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public ResultObject<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.provinceSwitchService.removeByIds(Arrays.asList(ids.split(",")));
		return ResultObject.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public ResultObject<?> queryById(@RequestParam(name="id",required=true) String id) {
		ProvinceSwitch provinceSwitch = provinceSwitchService.getById(id);
		if(provinceSwitch==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(provinceSwitch);
	}


}
